;;;
;;; SimpleBlink.S
;;; 
        
#define _SFR_ASM_COMPAT 1
#define __SFR_OFFSET 0

#include <avr/io.h>

#define PORT 5

.global main

;;;
;;; main - initialize port and blink a led
;;; 

main:           sbi     DDRB,PORT       ; configure pin 5 on port D as an output port
loop:           sbi     PORTB,PORT      ; turn the LED on
                ldi     r19,250         ; delay
                rcall   delay                
                cbi     PORTB,PORT      ; turn the LED off
                ldi     r19,250         ; delay
                rcall   delay
                rjmp    loop            ; loop forever

;;; 
;;; delay - delay for N milliseconds
;;;
;;;   The timing of this function only works on a 16Mhz CPU
;;;
;;;   r19 contains the number of milliseconds
;;;   r1/r2 will be used as work registers
;;; 

delay:
                
                ldi     r22,246           ; 1 N A
delay_2:        ldi     r21,15            ; 1 N A B
delay_1:        nop                       ; 1 N A B
                dec     r21               ; 1 N A B
                brne    delay_1           ; 1 N A B + (2 N A)        
                dec     r22               ; 1 N A
                brne    delay_2           ; 1 N A + (2 N)        
                dec     r19               ; 1 N
                brne    delay             ; 1 N + 2
                ret                       ; 4
